﻿using AssetsSys.BLL.AutoMapper;
using AssetsSys.DAL;
using AssetsSys.Dto.Permission;
using AssetsSys.model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using AssetsSys.Dto;

namespace AssetsSys.BLL
{
    /// <summary>
    /// 用户权限
    /// </summary>
    public class PermissionManger
    {
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <returns></returns>
        public static async Task<List<PermissionModel>> GetList()
        {
            using (var perSvc = new PermissionService())
            {
                var permission = await perSvc.GetAll().OrderByDescending(x => x.CreateTime).ToListAsync();
                return permission.MapTo<List<PermissionModel>>();
            }
        }

        /// <summary>
        /// 获取没有管理员的列表
        /// </summary>
        /// <returns></returns>
        public static async Task<List<PermissionModel>> GetListWithoutAdmin()
        {
            using (var perSvc = new PermissionService())
            {
                var permission = await perSvc.GetAll(x=>x.PermissionName!="管理员").OrderByDescending(x => x.CreateTime).ToListAsync();
                return permission.MapTo<List<PermissionModel>>();
            }
        }

        /// <summary>
        /// 新增权限
        /// </summary>
        /// <param name="permissionName"></param>
        /// <param name="permissionMenu"></param>
        /// <param name="permissionId"></param>
        /// <returns></returns>
        public static async Task<bool> Create(string permissionName, string permissionMenu,Guid permissionId)
        {
            using (var perSvc = new PermissionService())
            {
                await perSvc.CreateAsync(new Permission()
                {
                    Id = permissionId,
                    PermissionName = permissionName,
                    PermissionMenu = permissionMenu
                });
                return true;
            }
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<bool> Delete(Guid id)
        {
            using (var perSvc = new PermissionService())
            {
                var model = await perSvc.GetOne(id);
                if (model == null)
                {
                    return false;
                }
                await perSvc.Remove(id);
                return true;
            }
        }
        
        /// <summary>
        /// 获取权限详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<PermissionModel> GetById(Guid id)
        {
            using (var perSvc = new PermissionService())
            {
                return (await perSvc.GetOne(id)).MapTo<PermissionModel>();
            }
        }

        /// <summary>
        /// 变更权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<bool> Update(PermissionModel model)
        {
            using (var perSvc = new PermissionService())
            {
                var oldModel = await perSvc.GetOne(model.Id);
                if (oldModel == null)
                {
                    return false;
                }
                oldModel.PermissionName = model.PermissionName ?? oldModel.PermissionName;
                oldModel.PermissionMenu = model.PermissionMenu ?? oldModel.PermissionMenu;
                await perSvc.EditAsvnc(oldModel);
            }
            return true;
        }

        
    }
}
